package com.start.common.tools;

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.WordUtils;

import java.io.InputStream;
import java.util.*;

public class DataTableTools {
    private static final Map<String, String> avueTypeToMySQlType = new HashMap<>();
    private static final Map<String, String> defaultFieldMap = new HashMap<>();

    static {
        avueTypeToMySQlType.put("input", "varchar(255)");
        avueTypeToMySQlType.put("select", "varchar(255)");
        avueTypeToMySQlType.put("array", "varchar(255)");
        avueTypeToMySQlType.put("url", "varchar(255)");
        avueTypeToMySQlType.put("img", "varchar(255)");
        avueTypeToMySQlType.put("cascader", "varchar(255)");
        avueTypeToMySQlType.put("checkbox", "varchar(255)");
        avueTypeToMySQlType.put("datetime", "timestamp");
        avueTypeToMySQlType.put("time", "timestamp");
        avueTypeToMySQlType.put("dynamic", "json");
        avueTypeToMySQlType.put("icon", "varchar(255)");
        avueTypeToMySQlType.put("radio", "varchar(255)");
        avueTypeToMySQlType.put("map", "json");
        avueTypeToMySQlType.put("switch", "varchar(255)");
        avueTypeToMySQlType.put("number", "int");
        avueTypeToMySQlType.put("rate", "varchar(255)");
        avueTypeToMySQlType.put("slider", "int");
        avueTypeToMySQlType.put("color", "int");
        avueTypeToMySQlType.put("upload", "JSON");
        avueTypeToMySQlType.put("tree", "varchar(255)");
        avueTypeToMySQlType.put("table", "JSON");
        avueTypeToMySQlType.put("ueditor", "text");

        defaultFieldMap.put("id", "varchar(32) PRIMARY KEY COMMENT '主键'");
        defaultFieldMap.put("create_by", "varchar(32)  COMMENT '创建人'");
        defaultFieldMap.put("create_time", "timestamp COMMENT '创建时间'");
        defaultFieldMap.put("update_by", "varchar(32) COMMENT '修改人'");
        defaultFieldMap.put("update_time", "timestamp COMMENT '修改时间'");
    }

    public static JSONObject getDefaultTableColumn(InputStream inputStream) {
        final String read = IoUtil.read(inputStream, "UTF-8");
        return JSONObject.parseObject(read);
    }


    /**
     * 列名转换成Java属性名
     */
    public static String tableColumnToJava(String columnName) {
        return WordUtils.capitalizeFully(columnName, '_').replace("_", "");
    }

    /**
     * 表名转换成Java类名
     */
    public static String tableNameToJava(String tableName, String tablePrefix) {
        if (StringUtils.isNotBlank(tablePrefix)) {
            tableName = tableName.replaceFirst(tablePrefix, "");
        }
        return tableColumnToJava(tableName);
    }

    public static String avueOptionToColumnSQL(JSONObject avueOption) {
        JSONArray columns = avueOption.getJSONArray("column");
        List<String> columnSQL = new ArrayList<>();
        List<String> fields = new ArrayList<>();
        for (int i = 0; i < columns.size(); i++) {
            JSONObject column = columns.getJSONObject(i);
            String columnName = com.baomidou.mybatisplus.core.toolkit.StringUtils.underlineToCamel(column.getString("prop"));
            columnSQL.add(avueColumnOptionToSQL(column));
            fields.add(columnName);
        }
        Set<String> keySet = defaultFieldMap.keySet();
        for (String column : keySet) {
            if (fields.contains(column)) {
                continue;
            }
            fields.add(column);
            if ("id".equals(column)) {
                columnSQL.add(0, column + " " + defaultFieldMap.get(column));
            } else {
                columnSQL.add(1, column + " " + defaultFieldMap.get(column));
            }

        }
        return String.join(",", columnSQL);
    }

    public static String avueColumnOptionToSQL(JSONObject columnItem) {
        String type = columnItem.containsKey("type") ? columnItem.getString("type") : "input";
        type = columnItem.containsKey("component") ? columnItem.getString("component") : type;
        String sqlType = avueTypeToMySQlType.getOrDefault(type, "varchar(255)");
        String comment = StringUtils.isNotBlank(columnItem.getString("label")) ? columnItem.getString("label") : columnItem.getString("prop");
        String columnName = com.baomidou.mybatisplus.core.toolkit.StringUtils.underlineToCamel(columnItem.getString("prop"));
        return columnName + " " + sqlType + " COMMENT '" + comment + "'";
    }

}
